package me.blog.whiteday910.smartscore.Menu01;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import me.blog.whiteday910.smartscore.R;
import me.blog.whiteday910.smartscore.SharedValues;
import me.blog.whiteday910.smartscore.lib.DraggableGridView.DraggableGridView;
import me.blog.whiteday910.smartscore.lib.DraggableGridView.OnRearrangeListener;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;

public class CreateScoreActivity extends Activity
{
	DraggableGridView dgv;
	Button createScoreBtn01_SortOk;

	ScrollView testscrollv;
	ImageView testImageView01;

	ArrayList<CustomGallery> array01_Pictures;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_createscore);

		dgv = (DraggableGridView) findViewById(R.id.dgv);
		testscrollv = (ScrollView) findViewById(R.id.testscrollv);
		testImageView01 = (ImageView) findViewById(R.id.testImageView01);

		createScoreBtn01_SortOk = (Button) findViewById(R.id.createScoreBtn01_SortOk);
		createScoreBtn01_SortOk.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (array01_Pictures != null && array01_Pictures.size() > 0)
				{
					int count = array01_Pictures.size();

					int widthOfImage = 0;
					int totalHeightOfImage = 0;

					for (int i = 0; i < count; i++)
					{
						CustomGallery oneGallery = array01_Pictures.get(i);

						
						///  이미지 메모리 관련 로직 수정 필요
						
						BitmapFactory.Options options = new BitmapFactory.Options();
						options.inJustDecodeBounds = true;
						BitmapFactory.decodeFile(oneGallery.sdcardPath, options);
						if (widthOfImage < options.outWidth)
						{
							widthOfImage = options.outWidth;
						}
						totalHeightOfImage += options.outHeight;

					}

					Bitmap bitmap = Bitmap.createBitmap(widthOfImage, totalHeightOfImage, Bitmap.Config.RGB_565);
					
					Canvas canvas = new Canvas(bitmap);
					canvas.drawRGB(255, 255, 255);
					int prevHeight = 0;
					for (int i = 0; i < count; i++)
					{
						CustomGallery oneGallery = array01_Pictures.get(i);
						Bitmap oneBitmap = getBitmapFromPath(oneGallery.sdcardPath);
						canvas.drawBitmap(oneBitmap, 0, prevHeight, null);
						prevHeight += oneBitmap.getHeight();
					}

					testImageView01.setImageBitmap(bitmap);

					testscrollv.setVisibility(View.VISIBLE);

				}
			}
		});

		setListeners();

		// 진입시 바로 사진 고르기
		Intent i = new Intent(SharedValues.ACTION_MULTIPLE_PICK);
		startActivityForResult(i, 200);

	}

	private void setListeners()
	{

		dgv.setOnRearrangeListener(new OnRearrangeListener()
		{
			public void onRearrange(int oldIndex, int newIndex)
			{
				if (array01_Pictures != null && oldIndex > -1 && newIndex > -1)
				{
					Log.d("DLog", "어레이 사이즈 : " + array01_Pictures.size() + " oldIndex : " + oldIndex + "  newIndex : " + newIndex);
					Collections.swap(array01_Pictures, oldIndex, newIndex);
				}

			}
		});
		dgv.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				/*
				 * dgv.removeViewAt(arg2); poem.remove(arg2);
				 */
			}
		});

	}

	private Bitmap getBitmapFromPath(String path)
	{
		File imgFile = new File(path);
		if (imgFile.exists())
		{
			Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
			return myBitmap;
		}
		return null;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 200 && resultCode == Activity.RESULT_OK)
		{

			String[] all_path = data.getStringArrayExtra("all_path");
			array01_Pictures = new ArrayList<CustomGallery>();

			for (String string : all_path)
			{
				CustomGallery item = new CustomGallery();
				item.sdcardPath = string;
				item.bitmapImage = getBitmapFromPath(string);
				array01_Pictures.add(item);
				
				ImageViewTopCrop imageView = new ImageViewTopCrop(CreateScoreActivity.this);
				imageView.setImageBitmap(item.bitmapImage);

				dgv.addView(imageView);
			}

		} else
		{

		}
	}
}
